﻿function ok() {
    layer.closeAll();
    txttip('修改成功', function () { location.href = '/my/weixin_info'; });
}
function err() {
    layer.closeAll();
    txttip('二次密码错误');
}
document.querySelector('body').addEventListener('touchmove', function (ev) {
    // 判断默认行为是否可以被禁用
    if (event.cancelable) {
        // 判断默认行为是否已经被禁用
        if (!event.defaultPrevented) {
            event.preventDefault();
        }
    }
});
$(function () {
    var result = document.getElementById("result");
    var input = document.getElementById("file_input");

    if (typeof FileReader === 'undefined') {
        result.innerHTML = "抱歉，你的浏览器不支持 FileReader";
        input.setAttribute('disabled', 'disabled');
    } else {
        input.addEventListener('change', readFile, false);
    }
});
function start() {
    document.getElementById('file_input').click();
}
function readFile() {
    var file = this.files[0];
    //alert(file.type);
    //if (!/image\/\w+/.test(file.type)) {
    //    alert("请确保文件为图像类型");
    //    return false;
    //}
    var reader = new FileReader();
    reader.readAsDataURL(file);

    reader.onload = function (e) {
        //alert(3333)
        //alert(this.result);
        var image = new Image();
        var resized;
        image.src = this.result;
        image.onload = function () {
            resized = resizeMe(image);
            result.innerHTML = '<img id="image" src="' + resized + '" style="max-width:100%;max-height:300px;" alt=""/>';
            $('#sbtn').html('　<button style="border:0px;padding:8px 20px;background:#ff006e;color:#fff;" onclick="rota()">旋转</button>　<button type="button" style="border:0px;padding:8px 20px;color:#fff;;background:#3F75FF;" onclick="save()">提交数据</button>');
            $('#image').cropper({
                viewMode: 1,
                dragMode: 'move',
                autoCropArea: 1,
                restore: false,
                guides: false,
                highlight: false,
                cropBoxMovable: false,
                cropBoxResizable: false
            });
        }
    }
}
function resizeMe(img) {
    //压缩的大小
    var max_width = 400;
    var max_height = 800;

    var canvas = document.createElement('canvas');
    var width = img.width;
    var height = img.height;

    if (width > height) {
        if (width > max_width) {
            height = Math.round(height *= max_width / width);
            width = max_width;
        }
    }
    else {
        if (height > max_height) {
            width = Math.round(width *= max_height / height);
            height = max_height;
        }
    }

    canvas.width = width;
    canvas.height = height;

    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0, width, height);
    //压缩率
    return canvas.toDataURL("image/jpeg", 0.8);
}
function rota() {
    $('#image').cropper("rotate", 90);
}
function save() {
    if ($('#pass').val() == '') {
        txttip('请填写二次密码');
        return;
    }
    if ($('#weixin_no').val() == '') {
        txttip('您尚未设置微信号');
        return;
    }
    loading();
    var $imgData = $('#image').cropper('getCroppedCanvas');
    var dataurl = $imgData.toDataURL('image/jpg');
    $.post('/dos_m/member_weixin_save', { data: dataurl, no: $('#weixin_no').val(), pass: $('#pass').val() }, function (d) { eval(d); });
}